package cn.net.wanji.config;

import cn.hutool.core.lang.Assert;
import cn.net.wanji.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guodejun
 * @createTime 2022/11/1 14:15
 * @description
 */
@Configuration
public class ApplicationConfig {

    @Value("${file.savePath}")
    private String fileSavePath;

    private String[] fileSavePaths = null;

    @Value("${file.nginx}")
    private String fileNginx;

    private String[] fileNginxs = null;

    @Value("${file.disc-shell}")
    private String discShell;

    @Value("${mock.confidence-degree}")
    private boolean mockConfidenceDegree;

    @Value("${system.type}")
    private String systemType;

    @Value("${data-copy.is-master}")
    private boolean dataCopyIsMaster;

    @Value("${data-copy.slave-path}")
    private String dataCopySlavePath;
    @Value("${file.disc-percentage}")
    private Double discPercentage;
    @Value("${log.path}")
    private String logPath;
    public String getFileSavePath(String recordId) {

        // 首次调用 初始化 fileSavePaths
        if (fileSavePaths == null) {
            fileSavePaths = fileSavePath.split(";");
        }
        int index = getIndexByHashCode(recordId, fileSavePaths.length);
        return fileSavePaths[index];
    }

    public String getFileNginx(String recordId) {
        if (fileNginxs == null) {
            fileNginxs = fileNginx.split(";");
        }
        int index = getIndexByHashCode(recordId, fileNginxs.length);
        return fileNginxs[index];
    }

    public boolean getMockConfidenceDegree() {
        return mockConfidenceDegree;
    }

    private final List<String> shell = new ArrayList<>();

    public List<String> getShell() {
        if (shell.size() == 0 && !StringUtils.isEmpty(discShell)) {
            shell.add(discShell);
        }
        return shell;
    }

    public String getSystemType() {
        return this.systemType;
    }

    public boolean getDataCopyIsMaster() {
        return this.dataCopyIsMaster;
    }

    public String getDataCopySlavePath() {
        return this.dataCopySlavePath;
    }

    private static int getIndexByHashCode(String recordId, int arrayLen) {
        int hashCode = Math.abs(recordId.hashCode());
        return hashCode % arrayLen;
    }
    public Double getDiscPercentage() {
        return discPercentage;
    }
    public String getLogPath() {
        return logPath;
    }
}
